<?php
/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/6/6
 * Time: 17:38
 */

namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use App\Models\Article;
use Illuminate\Database\Eloquent\Model;
use Cache;

class User extends Model
{  
    public $timestamps = false;
    protected $table = 'wx_user';

    /**
     * @param array $map
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws ApiException
     */
    public function getUserinfo($map = [])
    {
        $userinfo = false;

        if (intval($this->cache_on) >= 1 && isset($map['id']))
        {
            $userinfo = Cache::get('userinfo_' . $map['id']);
        }
        if (!$userinfo || empty($userinfo))
        {
            try
            {
                if (isset($map['id'])) unset($map['rid']);
                
                $field = ['id','phone','user_name','gender','user_avatar','user_state','push_client_id'];

                $userinfo = $this->select($field)->where($map)->firstOrFail();
                $userinfo = json_decode(json_encode($userinfo, true), true);
                $recordInfo = (new \App\Models\CacheModel\Recode())->getRecodeLists($userinfo['id'], 1);
                if ($recordInfo)
                {
                    $userinfo['rid'] = (int)($recordInfo['id'] ?? 0);
                    $userinfo['user_name'] = $recordInfo['name'] ?? '';
                    $userinfo['user_avatar'] = $recordInfo['avatar'] ?? '';
                } else
                {
                    $userinfo['rid'] = 0;
                }

                Cache::set('userinfo_' . $userinfo['id'], $userinfo, 86400 * 3);

            }
            catch (\Exception $e)
            {
                throw new ApiException('用户信息获取失败,请重新登录', 200, 403);
            }
        }

        return $userinfo;
    }

    /**
     * 更新用户信息
     * @param array $map
     * @param array $data
     * @throws ApiException
     */
    public function updateUserinfo($map = [], $data = [])
    {
        if (!empty($map) && !empty($data))
        {
            try
            {
                $this->where($map)->update($data);

                Cache::forget('userinfo_' . $map['id']);
            }
            catch (\Exception $e)
            {
                throw new ApiException('更新用户信息失败', 200, 403);
            }
        }
    }
    // 一对多关联评论表
    public function comments()
    {
        return $this->hasMany('\App\Models\Comment','id','uid');
    }
    // 一对多关联评论表,获取回复我的评论
    public function rMyComments()
    {
        return $this->hasMany('App\Models\Comment','to_uid');
    }
     // 一对多关联评论表
    public function Commity_comments()
    {
        return $this->hasMany('App\Models\Api\Comment','id','uid');
    }
    // 一对多关联评论表,获取回复我的评论
    public function Commity_rMyComments()
    {
        return $this->hasMany('App\Models\Api\Comment','to_uid');
    }
     // 获得点赞过得话题
    public function likeArticles()
    {
        $article_ids = $this->hasMany('App\Models\Like')->where('likeable_type','App\Models\Article')->pluck('likeable_id');
        $articles = Article::whereIn('id', $article_ids);
        return $articles;
    }

      // 获得评论过得话题
    public function likeComments($user_id)
    {
        $article_ids = $this->hasMany('App\Models\Comment')->where('uid',$user_id)->pluck('article_id');
        $articles = Article::whereIn('id', $article_ids);
        return $articles;
    }
    
       // 获得评论过得话题
    public function Commity_likeComments($user_id)
    {
        $article_ids = $this->hasMany('App\Api\Models\Comment')->where('uid',$user_id)->pluck('community_id');
        $articles = Article::whereIn('id', $article_ids);
        return $articles;
    }
    /**
     * @param array $map
     * @throws ApiException
     */
    public function updateRegid($map = [])
    {
        if ($map['uid'] && $map['regid'])
        {
            $userinfo = $this->getUserinfo(['id'=>$map['uid']]);
            // 有变化  进行更新
            if($userinfo['push_client_id'] != $map['regid'])
            {
                $userinfo['push_client_id'] = $map['regid'];
                try
                {
                    $this->where(['id'=>$map['uid']])->update(['push_client_id'=>$map['regid']]);

                    Cache::set('userinfo_' . $userinfo['id'], $userinfo, 86400 * 3);
                }
                catch (\Exception $e)
                {
                    throw new ApiException('更新用户信息失败', 200, 403);
                }
            }
        }
        return;
    }
}